﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Data.Entity;
using System.Web.Mvc;
using KSUNO_EMS.Models;

namespace KSUNO_EMS.Controllers
{
    public class ScheduleController : EmsController
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetSchedule(DateTime date)
        {
            var firstDayOfWeek = date.Subtract(new TimeSpan((int)date.DayOfWeek, 0, 0, 0));
            var lastDayOfWeek = date.Add(new TimeSpan(7 - (int)date.DayOfWeek, 0, 0, 0));

            return new ProperJsonResult
                       {
                           Data = new AjaxResult(true,
                                                 new
                                                     {
                                                         Date = firstDayOfWeek,
                                                         Schedules = Database.Employees.ToList()
                                                     .Select(employee =>
                                                         EmployeeScheduleDTO.FromDatabase(employee, employee.ScheduledDateTimes
                                                                 .Where(s => s.StartDateTime >= firstDayOfWeek && s.StartDateTime <= lastDayOfWeek)))
                                                     }),
                                                 //      Data = new AjaxResult(true,
                                                 //new
                                                 //    {
                                                 //        Date = firstDayOfWeek,
                                                 //        Schedules = Database.Employees.ToList()
                                                 //    .Select(employee =>
                                                 //            new
                                                 //                {
                                                 //                    Id = employee.Id,
                                                 //                    Employee = employee.FirstName + " " + employee.LastName,
                                                 //                    Schedule = employee.ScheduledDateTimes
                                                 //                .Where(s => s.StartDateTime >= firstDayOfWeek && s.StartDateTime <= lastDayOfWeek)
                                                 //                .Select(s => new { StartDate = s.StartDateTime, EndDate = s.EndDateTime })
                                                 //                })
                                                 //    }),
                           HasDates = true
                       };
        }
    }
}
